
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TrainingsApp.Model;
using TrainingsApp.Helper;
using WisDragon.Framework.Data.DataAccess;

namespace TrainingsApp.DAL {
    public class TS_Preview_Result_DAL {



        public static void TS_Preview_Result_UpdateGroupId(List<TS_Preview_Result_Entity> list) {

            foreach (var listItem in list) {
                TS_Preview_Result_UpdateGroupId(listItem);
            }
        }

        public static void TS_Preview_Result_UpdateSort(TS_Preview_Result_Entity entity) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_UpdateSort");
                cmd.SetParameterValue("@PreviewId", entity.PreviewId);
                cmd.SetParameterValue("@UserId", entity.UserId);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

        }

        public static void TS_Preview_Result_GroupUp(TS_Preview_Result_Entity entity) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_GroupUp");
                cmd.SetParameterValue("@PreviewId", entity.PreviewId);
                cmd.SetParameterValue("@UserId", entity.UserId);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

        }


        public static void TS_Preview_Result_UpdateGroupId(TS_Preview_Result_Entity entity) {

            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_UpdateGroupId");
                cmd.SetParameterValue("@PreviewId", entity.PreviewId);
                cmd.SetParameterValue("@GroupId", entity.GroupId);
                cmd.SetParameterValue("@Sort", entity.Sort);
                cmd.SetParameterValue("@UserId", entity.UserId);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

        }


        public static int TS_Preview_Result_MaxGroupId(Guid previewId) {


            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_MaxGroupId");
                cmd.SetParameterValue("@PreviewId", previewId);

                object o = cmd.ExecuteScalar<object>();
                if (o is DBNull) return -1;
                else return Convert.ToInt32(o);
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return 0;
        }

        public static List<TS_Preview_Result_Entity> TS_Preview_Result_ListByPreviewId(Guid previewId) {

            List<TS_Preview_Result_Entity> list = new List<TS_Preview_Result_Entity>();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_ListByPreviewId");
                cmd.SetParameterValue("@PreviewId", previewId);

                list = cmd.ExecuteEntityList<TS_Preview_Result_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return list;

        }


        public static List<TS_Preview_Result_Entity> TS_Preview_Result_ListByPreviewId(Guid previewId, long classId) {

            List<TS_Preview_Result_Entity> list = new List<TS_Preview_Result_Entity>();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_ListByPreviewIdAndClassId");
                cmd.SetParameterValue("@PreviewId", previewId);
                cmd.SetParameterValue("@ClassId", classId);

                list = cmd.ExecuteEntityList<TS_Preview_Result_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return list;

        }


        public static TS_Preview_Result_Collecion TS_Preview_Result_ListByPaging(int pageSize, int pageIndex) {
            TS_Preview_Result_Collecion list = new TS_Preview_Result_Collecion();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_ListByPaging");
                cmd.SetParameterValue("@pageSize", pageSize);
                cmd.SetParameterValue("@pageIndex", pageIndex);
                List<TS_Preview_Result_Entity> pageList = cmd.ExecuteEntityList<TS_Preview_Result_Entity>();
                list.TS_Preview_Result_List = pageList;
                list.TotalCount = Convert.ToInt32(cmd.GetParameterValue("@recordCount"));
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return list;

        }

        public static TS_Preview_Result_Entity TS_Preview_Result_View(System.Guid PreviewId, System.Int64 UsreId) {
            TS_Preview_Result_Entity c = default(TS_Preview_Result_Entity);
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_View");
                cmd.SetParameterValue("@PreviewId", PreviewId);
                cmd.SetParameterValue("@UsreId", UsreId);
                c = cmd.ExecuteEntity<TS_Preview_Result_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
            return c;
        }

        public static TS_Preview_Result_Entity TS_Preview_Result_Add(TS_Preview_Result_Entity entity) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_Add");
                cmd.SetParameterValue("@PreviewId", entity.PreviewId);
                cmd.SetParameterValue("@UsreId", entity.UserId);
                cmd.SetParameterValue("@QuestionId", entity.QuestionId);
                cmd.SetParameterValue("@Result", entity.Result);
                cmd.SetParameterValue("@AnswerDate", entity.AnswerDate);
                cmd.SetParameterValue("@GroupId", entity.GroupId);
                cmd.SetParameterValue("@GroupDate", entity.GroupDate);
                cmd.SetParameterValue("@Sort", entity.Sort);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return entity;
        }

        public static void TS_Preview_Result_Update(TS_Preview_Result_Entity entity) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_Update");
                cmd.SetParameterValue("@PreviewId", entity.PreviewId);
                cmd.SetParameterValue("@UsreId", entity.UserId);
                cmd.SetParameterValue("@QuestionId", entity.QuestionId);
                cmd.SetParameterValue("@Result", entity.Result);
                cmd.SetParameterValue("@AnswerDate", entity.AnswerDate);
                cmd.SetParameterValue("@GroupId", entity.GroupId);
                cmd.SetParameterValue("@GroupDate", entity.GroupDate);
                cmd.SetParameterValue("@Sort", entity.Sort);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
        }

        public static void TS_Preview_Result_Delete(System.Guid PreviewId, System.Int64 UsreId) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_Delete");
                cmd.SetParameterValue("@PreviewId", PreviewId);
                cmd.SetParameterValue("@UsreId", UsreId);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
        }

        public static List<TS_Preview_Result_Entity> TS_Preview_Result_GetTypicalByPreviewId(Guid PreviewId) {
            long count = 0;
            List<TS_Preview_Result_Entity> list = new List<TS_Preview_Result_Entity>();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_GetTypicalByPreviewId");
                cmd.SetParameterValue("@PreviewId", PreviewId);
                cmd.SetParameterValue("@ResultCount", count);
                list = cmd.ExecuteEntityList<TS_Preview_Result_Entity>();
                count = Convert.ToInt64(cmd.GetParameterValue("@ResultCount"));
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return list;
        }

        public static List<TS_Preview_Result_Entity> TS_Preview_Result_GetTypicalByPreviewIdAndClassId(Guid previewId, long classId) {
            long count = 0;
            List<TS_Preview_Result_Entity> list = new List<TS_Preview_Result_Entity>();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_GetTypicalByPreviewIdAndClassId");
                cmd.SetParameterValue("@PreviewId", previewId);
                cmd.SetParameterValue("@ClassId", classId);
                cmd.SetParameterValue("@ResultCount", count);
                list = cmd.ExecuteEntityList<TS_Preview_Result_Entity>();
                count = Convert.ToInt64(cmd.GetParameterValue("@ResultCount"));
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return list;
        }

        public static List<TS_Preview_Result_Entity> TS_Preview_Result_GetTypicalByPreviewId2(Guid PreviewId, out long resultCount) {
            resultCount = 0;
            List<TS_Preview_Result_Entity> list = new List<TS_Preview_Result_Entity>();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_GetTypicalByPreviewId");
                cmd.SetParameterValue("@PreviewId", PreviewId);
                cmd.SetParameterValue("@ResultCount", resultCount);
                list = cmd.ExecuteEntityList<TS_Preview_Result_Entity>();
                resultCount = Convert.ToInt64(cmd.GetParameterValue("@ResultCount"));
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return list;
        }

        public static int TS_Preview_Result_GetCountByPreviewId(Guid PreviewId) {
            int recordCount = 0;
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_GetCountByPreviewId");
                cmd.SetParameterValue("@PreviewId", PreviewId);
                cmd.ExecuteNonQuery();
                recordCount = Convert.ToInt32(cmd.GetParameterValue("@recordCount"));
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return recordCount;

        }


        public static List<TS_Preview_Entity> TS_Preview_Result_TopByTeacher(long userId) {

            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_TopByTeacher");
                cmd.SetParameterValue("@UserId", userId);
                return cmd.ExecuteEntityList<TS_Preview_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return new List<TS_Preview_Entity>(0);
        }


        public static List<TS_Preview_Entity> TS_Preview_Result_TopByStudent(long userId) {

            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_Preview_Result_TopByStudent");
                cmd.SetParameterValue("@UserId", userId);
                return cmd.ExecuteEntityList<TS_Preview_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return new List<TS_Preview_Entity>(0);
        }
    }
}
